package cn.wen.yaling.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wen.yaling.commons.enums.CommentType;
import cn.wen.yaling.commons.enums.HttpCode;
import cn.wen.yaling.commons.vo.CommentChildVO;
import cn.wen.yaling.commons.vo.CommentListVO;
import cn.wen.yaling.commons.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.CommentEntity;
import cn.wen.yaling.xo.service.CommentService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;



/**
 * 
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@Slf4j
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论或者回复评论 如果replyCommentId 为空则是评论 如果不为空则
     * 判断对象里 的parentId 是否为空 为空则是评论 如果不为空则是回复
     * @return
     */
    @PostMapping("/web/comment/publish")
    public ResultEntity commentInsert(@RequestBody CommentEntity commentEntity) {
        if (commentEntity.getCommentType() < 1 || commentEntity.getCommentType() >3 )
            return ResultEntity.error(HttpCode.内部服务器错误.getCode(), "发表评论失败！");
        boolean result = commentService.insertComment(commentEntity);
        if (!result) return ResultEntity.error(HttpCode.内部服务器错误.getCode(), "发表评论失败！");
        // TODO 需要调用消息队列去通知邮箱用户
        return ResultEntity.ok();
    }

    /*============================================后台 Api ==============================================*/
    @GetMapping("/admin/comment/list")
    public ResultEntity commentListByType(@RequestParam("type") Integer type) {
        List<CommentListVO> list = commentService.getCommentListByType(type);
        if (list == null) return ResultEntity.error();
        return ResultEntity.ok().put("data", list);
    }

    @GetMapping("/admin/comment/search/list")
    public ResultEntity searchCommentListByType(@RequestParam("type") Integer type,
                                                @RequestParam("keyword") String keyword) {
        List<CommentListVO> list = commentService.searchCommentListByType(type, keyword);
        if (list == null) return ResultEntity.error();
        return ResultEntity.ok().put("data", list);
    }

    @GetMapping("/admin/comment/child/list")
    public ResultEntity childCommentList(@RequestParam("parentId") Integer parentId) {
        List<CommentChildVO> list = commentService.getChildCommentList(parentId);
        if (list == null) return ResultEntity.error();
        return ResultEntity.ok().put("data", list);
    }

    @PostMapping("/admin/comment/delete")
    public ResultEntity deleteComment(@RequestParam("commentIds") List<Integer> commentIds) {
        boolean result = commentService.deleteCommentByIds(commentIds);
        if (!result) return ResultEntity.error(HttpCode.内部服务器错误.getCode(), "删除失败！");
        return ResultEntity.ok();
    }

    @PostMapping("/admin/comment/update-status")
    public ResultEntity updateCommentStatus(@RequestParam("commentId") Integer commentId,
                                            @RequestParam("status") Integer status) {
        boolean result = commentService.updateCommentStatusById(commentId, status);
        if (!result) return ResultEntity.error(HttpCode.内部服务器错误.getCode(), "修改失败");
        return ResultEntity.ok();
    }

}
